"""
@ProjectName : StudentManagementSystem
@File        : student_ui.py
@Author      : WorldWind(1772830365@qq.com)
@Address     : ChangSha - BoWeiFeng
@Date        : 2025/7/16 上午11:26
"""
from students.student import Student
from students.student_service import StudentService


# 直接负责用户交互
class StudentUI:
    def __init__(self):
        self.studentService = StudentService()
        print(f'This is __init__ method. {self.get_class_name()} class startup')

    def search(self):
        condition = input('请输入学生的学号或者姓名:s = 学号或name = 姓名:')
        if condition.startswith('s'):
            res = condition.split('=')
            sno = res[1]  # 第一个元素,就是姓名
            print(self.studentService.search(sno, None))
            return self.studentService.search(sno, None)
        else:
            res = condition.split('=')
            name = res[1]  # 第一个元素,就是姓名
            return self.studentService.search(None, name)

    def add(self):
        content = input('请输入学生信息![学号,姓名,年龄,手机号,地址,性别]:')
        info = content.split(',')  # 分割成列表
        # 创建学生对象
        s1 = Student()
        s1.sno = info[0]
        s1.name = info[1]
        s1.age = int(info[2])
        s1.phone = info[3]
        s1.address = info[4]
        s1.sex = info[5]
        count = self.studentService.add(s1)
        if count == 1:
            print('添加成功')
        else:
            print('添加失败')

    def update(self):
        content = input('请输入学生信息![学号,姓名,年龄,手机号,地址,性别,id]:')
        info = content.split(',')  # 分割成列表
        # 创建学生对象
        s1 = Student()
        s1.sno = info[0]
        s1.name = info[1]
        s1.age = int(info[2])
        s1.phone = info[3]
        s1.address = info[4]
        s1.sex = info[5]
        s1.id = info[6]
        count = self.studentService.update(s1)
        if count == 1:
            print('更新成功')
        else:
            print('更新失败')

    # 根据学号删除
    def delete(self):
        sno = input('请输入您想删除学生信息的学生学号:')
        # 执行删除操作
        count = self.studentService.delete(sno)
        if count > 0:
            print('删除成功')
        else:
            print('删除失败')

    @classmethod
    def get_class_name(cls):
        return cls.__name__

    def __del__(self):
        print(f'This is __del__ method. The {self.get_class_name()} class has been recycled')


if __name__ == '__main__':
    ui = StudentUI()
    # ui.search()
    # ui.add()
    # ui.delete('s007')
    # ui.update()
